/*
@Author   : Uyynot
@Email    : uyynot@qq.com
@Time     : 2023/8/25 16:53
@File     : delete.go
@Project  : web_scaff
@Desc     :
*/
package user

import (
	"fmt"
	"web_scaff/models/response"
	sysValidator "web_scaff/pkg/validator"
	serviceUser "web_scaff/service/user"

	"github.com/gin-gonic/gin"
	"github.com/go-playground/validator/v10"
	"go.uber.org/zap"
)

type reqDelete struct {
	Id string `json:"id" uri:"id" binding:"required"`
}

func Delete(c *gin.Context) {
	var req = new(reqDelete)

	if err := c.ShouldBindUri(req); err != nil {
		// 请求参数有误，直接返回响应
		zap.L().Error("删除用户失败", zap.Error(err))
		// 判断err是不是validator.ValidationErrors 类型
		errs, ok := err.(validator.ValidationErrors)
		if !ok {
			response.FailWithMsg("删除用户失败", c)
			return
		}
		response.FailWithMsg(sysValidator.RemoveTopStruct(errs.Translate(sysValidator.Trans)), c)
		return
	}
	fmt.Println("REQ", req)

	if err := serviceUser.Delete(req.Id); err != nil {
		response.FailWithMsg(err.Error(), c)
		return
	}
	response.Success(c)
}
